<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容 - 头部 -->
      <el-card class="tree-card">
        <!-- 放置结构内容 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 放置一个el-tree -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 -->
          <tree-tools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>
      <!-- /组织架构内容 - 头部 -->
    </div>
    <!-- 放置新增弹层组件 -->
    <add-ddept v-if="showDialog" ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
    <!-- /放置新增弹层组件 -->
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDdept from './components/add-dept.vue'
export default {
  components: {
    treeTools,
    AddDdept
  },
  data() {
    return {
      // 头部数据结构
      company: {},
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      node: null // 记录点前点击的node节点
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      // this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.company = { name: '湖南湘里别来恰饭', manager: '负责人', id: '' }
      this.departs = tranListToTreeData(res.depts, '')
      // console.log(res)
    },
    addDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node
    },
    editDepts(node) {
      this.showDialog = true // 弹出层
      this.node = node
      this.$nextTick(() => {
        this.$refs.addDept.getDepartDetail(node.id)
      })
    }
  }
}
</script>

<style scoped>
.tree-card {
padding: 30px 140px;
font-size: 14px;
}
</style>
